﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using caedisi.Controller.Interface;
using System.ComponentModel.Composition;
using caedisi.Controller;
using caedisi.Web.DataModel;
using caedisi.Common;
using System.Linq;

namespace caedisi.Models
{
    /// <summary>
    /// A singleton who contains all global variables. However the loading of all Lovs are explicitly initiated by the Bootstrapper
    /// rather than within the constructor of the Singleton instance. The reason is that the load of LOVs happing asynchronously and
    /// we only show the homepage to the user only after all Loading of LOVS are completed.
    /// </summary>
    public class Globals
    {
        private static volatile Globals instance;
        private static object syncRoot = new Object();

        /// <summary>
        /// Gets the instance.
        /// </summary>
        public static Globals Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                            instance = new Globals();
                    }
                }
                return instance;
            }
        }

        /// <summary>
       /// Prevents a default instance of the <see cref="Globals"/> class from being created.
        /// </summary>
        private Globals()
        {
            Lovs = new LovEntities();

            //SetupController.Instance.LoadAllLovs(
            //    entitiesFromServer =>
            //    {
            //        Lovs.CellStateTypes = entitiesFromServer.CellStateTypes;
            //        Lovs.NeighbourhoodTypes = entitiesFromServer.NeighbourhoodTypes;
            //        Lovs.Configurations = entitiesFromServer.Configurations;
            //    }
            //);
        }

        public LovEntities Lovs { get; private set; }


        /// <summary>
        /// Gets the default immunity time.
        /// </summary>
        public int DefaultImmunityTime
        {
            get
            {
                Configuration configurationRow = this.Lovs.Configurations.Where(config => config.key == Constant.KeyDefaultImmunityTime).First();
                if (configurationRow != null)
                    return int.Parse(configurationRow.Value);
                return 0;
            }
        }

        /// <summary>
        /// Gets the default radius visibility.
        /// </summary>
        public int DefaultRadiusVisibility
        {
            get
            {
                Configuration configurationRow = this.Lovs.Configurations.Where(config => config.key == Constant.KeyDefaultRadiusVisibility).First();
                if (configurationRow != null)
                    return int.Parse(configurationRow.Value);
                return 1;
            }
        }

        /// <summary>
        /// Gets the default radius contamination.
        /// </summary>
        public int DefaultRadiusContamination
        {
            get
            {
                Configuration configurationRow = this.Lovs.Configurations.Where(config => config.key == Constant.KeyDefaultRadiusContamination).First();
                if (configurationRow != null)
                    return int.Parse(configurationRow.Value);
                return 1;
            }
        }


        public static GridLength cellularAutomataGridMode_RowHeight = new GridLength(40.0);
        public static GridLength cellularAutomataGridMode_ColumnWidth = new GridLength(40.0);

        public static GridLength cellularAutomataGridMode_RowHeightt = new GridLength(80.0);
        public static GridLength cellularAutomataGridMode_ColumnWidthh = new GridLength(80.0);

        public static GridLength cellularAutomataGraphMode_EllipseHeight = new GridLength(20.0);
        public static GridLength cellularAutomataGraphMode_EllipseWidth = new GridLength(20.0);

       


        public static GridLength TransitionRuleControl_GridTransitionRules_RowHeight =  new GridLength(40.0);
        public static GridLength TransitionRuleControl_GridTransitionRules_ColumnWidth  = new GridLength(40.0);


        /// <summary>
        /// Loads the data.
        /// </summary>
        public void Create(Action<bool> Completed) 
        {
            SetupController.Instance.LoadAllLovs(
                entitiesFromServer =>
                {
                    Lovs.CellStateTypes = entitiesFromServer.CellStateTypes;
                    Lovs.NeighbourhoodTypes = entitiesFromServer.NeighbourhoodTypes;
                    Lovs.Configurations = entitiesFromServer.Configurations;
                    Completed(true);
                }
            );
        
        }

        /// <summary>
        /// Gets the default width of the new cellular automata.
        /// </summary>
        /// <value>
        /// The default width of the new cellular automata.
        /// </value>
        public short DefaultNewCellularAutomataWidth
        {
            get
            {
                Configuration configurationRow = this.Lovs.Configurations.Where(config => config.key == Constant.KeyDefaultNewCellularAutomataWidth).First();
                if (configurationRow != null)
                    return short.Parse(configurationRow.Value);
                return 1;
            }
        }

        /// <summary>
        /// Gets the default height of the new cellular automata.
        /// </summary>
        /// <value>
        /// The default height of the new cellular automata.
        /// </value>
        public short DefaultNewCellularAutomataHeight {
            get
            {
                Configuration configurationRow = this.Lovs.Configurations.Where(config => config.key == Constant.KeyDefaultNewCellularAutomataHeight).First();
                if (configurationRow != null)
                    return short.Parse(configurationRow.Value);
                return 1;
            }
        }
    }
}
